<?php
namespace Tongcheng\Controller;
use Think\Controller;
class AdvController extends CommonController {
    public function index(){
    	$model = M("Adv");
    	$map['wid'] = session('wid');
    	$list = $model->where($map)->select();
        $this->assign('list',$list);

        $adv = array("index"=>"首页","shop"=>'好店',"house"=>'房产页',"job"=>'招聘页',"pinche"=>'拼车页');
        $this->assign('adv',$adv);
        $this->display();
    }

    public function add(){
        if(IS_AJAX){
            $model = M("Adv");
            $new['wid'] = session('wid');
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
    	$model = M("Adv");
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            // echo $model->getLastSql();
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function homeFlash ()
    {
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['type'] = 'home';
        $list = $model->where($map)->select();
        $this->assign('list',$list);

        $adv = array("home"=>"首页轮播");
        $this->assign('adv',$adv);
        $this->display();
    }

    //
    public function addHomeFlash(){
        if(IS_AJAX){
            $model = M("Adv");
            $new['wid'] = session('wid');
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    //
    public function editHomeFlash ()
    {
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            // echo $model->getLastSql();
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('addHomeFlash');
        }
    }

    //
    public function shopAdv() {
        $where['a.wid'] = session('wid');
        //$where['a.type'] = 1;
        $list = M('adv_shop')->alias('a')
                             //->field('a.*, b.name, b.logo_url, b.cate')
                             ->where($where)
                             //->join('tp_shop b on a.shop_id = b.shop_id')
                             ->order('a.status desc')
                             ->select();
        $this->assign('list', $list);
        $this->display();
    }

    //
    public function addShopAdv () {
        if ($_POST['name']) {
            $whereOr['name'] = ['like', '%'. $_POST['name']. '%'];
            $whereOr['cate'] = ['like', '%'. $_POST['name']. '%'];
            $whereOr['_logic'] = 'or';
            $where['_complex'] = $whereOr;
        }
        $where['wid'] = session('wid');
        $count = M('shop')->where($where)->count();
        $Page = new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();
        $list = M('shop')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('show', $show);
        $this->display();
    }
    
    //
    public function doAddShopAdv () {
        $wid = session('wid');
        $type = $_POST['type'];
        $url_type = $_POST['url_type'];
        $position = $_POST['position'];
        $data = array();
        $data['wid'] = $wid;
        $data['title'] = $_POST['title'];
        $data['position'] = $position;
        $data['url_type'] = $url_type;
        $data['createTime'] = date('Y-m-d H:i:s', time());
        $data['imgurl'] = $_POST['imgurl'];
        $data['status'] = $_POST['status'];
        if($url_type == 1){
            $data['type'] = $type;
            if($type == 1){
                $data['shop_id'] = $_POST['search_id'];
                $search_name = M('shop')->where(array('shop_id'=>$_POST['search_id']))->getField('name');
                $data['shop_name'] = $search_name;
                if (!$_POST['search_id'] || !$search_name) {
                    $this->error("请搜索并选择店铺！");
                }
            }else if($type == 2){
                $data['goods_id'] = $_POST['search_id'];
                $search_name = M('goods')->where(array('id'=>$_POST['search_id']))->getField('title');
                $data['goods_name'] = $search_name;
                if (!$_POST['search_id'] || !$search_name) {
                    $this->error("请搜索并选择商品！");
                }
            }else if($type == 3){
                $data['news_id'] = $_POST['search_id'];
                $search_name = M('news')->where(array('id'=>$_POST['search_id']))->getField('title');
                $data['news_name'] = $search_name;
                if (!$_POST['search_id'] || !$search_name) {
                    $this->error("请搜索并选择资讯！");
                }
            }
        }else{
            if (!$_POST['url']) {
                $this->error("请填写链接！");
            }
            $data['url'] = $_POST['url'];
        }
        if ($_POST['id']) {
            $row = M('adv_shop')->where(['id' => $_POST['id']])->save($data);
        } else {
            $row = M('adv_shop')->add($data);
        }
        if ($row) {
            $this->success("操作成功！");
        } else {
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function delShopAdv () {
        $model = M("adv_shop");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        if($model->where($map)->delete()){
            $this->success("删除成功！");
        } else {
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function editShopAdv () {
        $model = M("adv_shop");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        $arr = $model->where($map)->find();
//        if($_POST['id']){
//            if ($arr['status']) {
//                $save['status'] = 0;
//            } else {
//                $save['status'] = 1;
//            }
//            $row = $model->where($map)->save($save);
//            if ($row) {
//                $this->success("修改成功！");
//            } else {
//                $this->error("网络忙，请稍后再试！");
//            }
//        }
        $this->assign('vo',$arr);
        $this->display('addShopAdv');
    }
    public function search(){
        header("Content-type: text/html; charset=utf-8");
        $keyword = trim($_GET['keyword']);//关键词
        $type = $_GET['type'];
        $eq['wid'] = session('wid');
        if($type == 1){
            $eq['name'] = ['like',"%$keyword%"];
            $list = M('shop')->where($eq)->select();
            foreach($list as $key=>$value){
                $list[$key]['title'] = $value['name'];
                $list[$key]['img_url'] = $value['logo_url'];
                $list[$key]['id'] = $value['shop_id'];
            }
        }else if($type == 2){
            $eq['title'] = ['like',"%$keyword%"];
            $list = M('goods')->where($eq)->select();
        }else if($type == 3){
            $eq['title'] = ['like',"%$keyword%"];
            $list = M('news')->where($eq)->select();
            foreach($list as $key=>$value){
                //$list[$key]['title'] = $value['name'];
                $img_arr = explode(',',$value['pics']);
                $list[$key]['img_url'] = $img_arr[0];
            }
        }
//        $str = '';
//        foreach($list as $k=>$v){
//            $str .= "<option value=".$v['id']." img=\"{src:'".$v['img_url']."', css:'height:40px;width:40px;'}\">";
//            $str .= $value['title'];
//            $str .= "</option>";
//        }
        $this->ajaxReturn($list);
    }
}